using GraphProcessor;
using IQIGame.Onigao.Logic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace IQIGame.Onigao.GamePlay.Editors
{
    [System.Serializable, NodeMenuItem(LevelEditorConst.Execution + "/" + LevelEditorConst.ChangeService, typeof(LevelServiceGraph))]
    public class LevelServiceExecutionNode_ChangeService : LevelServiceExecutionNode
    {
        [Serializable]
        public class ServiceEditorNode
        {
            public BaseGraph graph;
            [LevelServiceEditorCustomProperty(LevelEditorConst.ServiceEditorNode, "服务列表", nameof(graph))]
            public int groupID;
        }

        protected override ServiceExecutionType mServiceType => ServiceExecutionType.ChangeService;

        public override string name => LevelEditorConst.ChangeService;
        [Output("目标", false)] public EditorLevelServiceTargetSetter tagOrMcid;
        public List<ServiceEditorNode> services;

        protected override LevelServiceCustomConfig CreateConfig()
        {
            var rConfig = new LevelServiceExecutionCustom_ChangeService()
            {
                services = this.services.Select((x) => x.groupID).ToList()
            };
            tagOrMcid = new EditorLevelServiceTargetSetter((x) => rConfig.tagOrMcid = x);

            return rConfig;
        }

        protected override void OnSync(EditorLevelServiceExecutionConfig newConfig, LevelServiceExecutionNode tempNode)
        {
            if (tempNode is LevelServiceExecutionNode_ChangeService rNode)
            {
                this.services = new List<ServiceEditorNode>(rNode.services);
            }
            if (newConfig.customConfig is LevelServiceExecutionCustom_ChangeService rConfig)
            {
                this.SyncTarget(rConfig.tagOrMcid);
            }
        }
    }
}